﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QL_Nha_Sach_Phuong_Nam.Quản_lý_nhà_cung_cấp.Quản_lý_thông_tin_nhà_cung_cấp
{
    public partial class FormNhacungcap : Form
    {

        CXulyNhacungcap xuly;
        string ma;


        public FormNhacungcap()
        {
            InitializeComponent();
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            FormEditNCC f = new FormEditNCC(0,"");
            f.ShowDialog();
            hienthidsnhacungcap(xuly.GetdsNhaCungCap());
        }

        private void btnedit_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in lvnhacungcap.SelectedItems)
            {
                ma=lvi.SubItems[0].Text;
            }
            FormEditNCC f = new FormEditNCC(1,ma);
            f.ShowDialog();
            hienthidsnhacungcap(xuly.GetdsNhaCungCap());
        }

        private void btnsearch_Click(object sender, EventArgs e)
        {
            if (txtten.Text == "")
            {
                hienthidsnhacungcap(xuly.GetdsNhaCungCap());
            }
            else
            {
                List<Kết_nối.NhaCungCap> NCC = xuly.TimNhaCungCapTheoTen(txtten.Text);
                if (NCC != null)
                {
                    lvnhacungcap.Items.Clear();
                    foreach (Kết_nối.NhaCungCap ncc in NCC)
                    {
                        if (ncc.Nhacungcapstatus == "live")
                        {
                            ListViewItem lvi = lvnhacungcap.Items.Add(ncc.NhaCungCapNo);
                            lvi.SubItems.Add(ncc.NhaCungCapName);
                            lvi.SubItems.Add(ncc.NhaCungCapAddress);
                            lvi.SubItems.Add(ncc.NhaCungCapPhoneNumber);
                            lvi.SubItems.Add(ncc.NhaCungCapEmail);
                        }
                    }
                }
                else
                {
                    lvnhacungcap.Items.Clear();
                    MessageBox.Show("Không có nhà cung cấp này !!!");
                }
            }
        }

        private void FormNhacungcap_Load(object sender, EventArgs e)
        {
            xuly = new CXulyNhacungcap();
            hienthidsnhacungcap(xuly.GetdsNhaCungCap());
        }

        private void hienthidsnhacungcap(List<Kết_nối.NhaCungCap> ds)
        {
            lvnhacungcap.Items.Clear();
            foreach (Kết_nối.NhaCungCap ncc in ds)
            {
                if (ncc.Nhacungcapstatus == "live")
                {
                    ListViewItem lvi = lvnhacungcap.Items.Add(ncc.NhaCungCapNo);
                    lvi.SubItems.Add(ncc.NhaCungCapName);
                    lvi.SubItems.Add(ncc.NhaCungCapAddress);
                    lvi.SubItems.Add(ncc.NhaCungCapPhoneNumber);
                    lvi.SubItems.Add(ncc.NhaCungCapEmail);
                }
            }
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in lvnhacungcap.SelectedItems)
            {
                string ma = lvi.SubItems[0].Text;
                xuly.XoaNhacungcap(ma);
            }

            hienthidsnhacungcap(xuly.GetdsNhaCungCap());
        }

    }
}
